package LoginUtil;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import persistent.*;

import LoginUtil.User.UserType;

import com.db4o.*;
import com.db4o.cs.Db4oClientServer;
import com.db4o.ext.DatabaseClosedException;
import com.db4o.ext.Db4oIOException;
import com.db4o.ext.InvalidPasswordException;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {

	

public void doGet(HttpServletRequest request, HttpServletResponse response) 
			           throws ServletException, java.io.IOException {
	try
	{	    
		String username = request.getParameter("usernamebox");
		String password = request.getParameter("passwordbox");
		
		ObjectContainer container = 
	        (ObjectContainer)request.getAttribute(Db4oServletListener.KEY_DB4O_SESSION);
		
		User user = new User(username,password);
		
		
		user.setFirstName("John");
		user.setLastName("Doe");
		user.setUserType(UserType.Admin);
		container.store(user);
		container.commit();
		
		user.setValid(false);
		
		System.out.println("Your user name is " + username);          
		   System.out.println("Your password is " + password);

		   try {
			   ObjectSet<User> result = container.queryByExample(user);
		   
			   // if the user is valid
			   if(result.size() > 0) {
				   user = result.next();
				   user.setValid(true);
			   }
			   
		   } catch (Db4oIOException e) {
			   System.out.println("IO Exception on querying the database\n");
			   System.out.println(e.getMessage());
		   } catch (DatabaseClosedException e) {
			   System.out.println(e.getMessage());
		   } catch (InvalidPasswordException e) {
			   System.out.println("Invalid Password!");
		   } finally {
		   }
		
		if(user.isValid()) {
			HttpSession session = request.getSession(true);	    
	        session.setAttribute("currentSessionUser",user); 
	        
	        
	        if(user.getUserType() == UserType.Doctor)
	        {
	      	  response.sendRedirect("doctorMain.jsp"); //logged-in page  for doctors
	        }
	        else if(user.getUserType() == UserType.Nurse)
	        {
	      	  response.sendRedirect("nurseMain.jsp"); //logged-in page for nurses
	        }
	        else if(user.getUserType() == UserType.Patient)
	        {
	      	  response.sendRedirect("patientMain.jsp"); //logged-in page for nurses
	        }
	        else if(user.getUserType() == UserType.Admin)
	        {
	      	  response.sendRedirect("adminMain.jsp"); //logged-in page for nurses
	        }
	        else
	        {
	      	  response.sendRedirect("userLogged.jsp"); //logged-in page 
	        }
	   }
		        
	   else 
	        response.sendRedirect("invalidLogin.jsp"); //error page 
			
		
		/*
	     UserBean user = new UserBean();
	     user.setUserName(request.getParameter("usernamebox"));
	     user.setPassword(request.getParameter("passwordbox"));
	
	     user = UserDAO.login(user);
		   		    
	     if (user.isValid())
	     {
		        
	          HttpSession session = request.getSession(true);	    
	          session.setAttribute("currentSessionUser",user); 
	          //set user type here
	          user.setType("doctor");
	          
	          if(user.getType().compareToIgnoreCase("doctor") == 0)
	          {
	        	  response.sendRedirect("doctorMain.jsp"); //logged-in page  for doctors
	          }
	          else if(user.getType().compareToIgnoreCase("nurse") == 0)
	          {
	        	  response.sendRedirect("nurseMain.jsp"); //logged-in page for nurses
	          }
	          else if(user.getType().compareToIgnoreCase("patient") == 0)
	          {
	        	  response.sendRedirect("patientMain.jsp"); //logged-in page for nurses
	          }
	          else if(user.getType().compareToIgnoreCase("ADMIN") == 0)
	          {
	        	  response.sendRedirect("adminMain.jsp"); //logged-in page for nurses
	          }
	          else
	          {
	        	  response.sendRedirect("userLogged.jsp"); //logged-in page 
	          }
	     }
		        
	     else 
	          response.sendRedirect("invalidLogin.jsp"); //error page 
	} 
	*/
	} catch (InvalidPasswordException e) {
		response.sendRedirect("invalidLogin.jsp"); //error page 
	} catch (Throwable theException) {
	     System.out.println(theException); 
	}
  }
}
